package com.topInterview;

/**
 * @Author: huangzhigao
 * @Date: 2022/2/12 21:52
 */
public class Leetcode7_reverseInteger {
    public int reverse(int x) {
        //x向右移动31位，正好移动到符号位，如果当前位 & 1==1则为负数
        boolean neg = ((x >>> 31) & 1) == 1;
        x = neg ? x : -x;
        int m = Integer.MIN_VALUE / 10;
        int o = Integer.MIN_VALUE % 10;
        int res = 0;
        while (x != 0) {
            if (res < m || res == m && res % 10 < o) {
                //res的值比最小值还小，取模后的值还小,溢出
                return 0;
            }
            res = res * 10 + x % 10;
            x = x / 10;
        }

        return neg ? res : Math.abs(res);
    }
}
